package com.bys.admin.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bys.admin.system.controller.vo.LoginVo;
import com.bys.admin.system.entity.SysUser;
import com.bys.admin.system.service.impl.LoginService;
import com.bys.response.R;
import com.bys.admin.system.controller.vo.SysUserVo;
import com.bys.admin.system.service.ISysUserService;
import com.bys.vo.PageVo;


import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bys
 * @since 2023-06-26
 */
@Tag(name = "用户接口")
@RestController
@RequestMapping("/system/sys-user")
@Validated
public class SysUserController {
    @Resource
    private ISysUserService sysUserService;

    @Resource
    private LoginService loginService;
    @PostMapping("/login")
    public R<Map<String,Object>> login(@RequestBody @Valid LoginVo loginVo){
        Map<String,Object> map  = loginService.login(loginVo.getUsername(), loginVo.getPassword());
        return R.success(map);
    }
    @Operation(summary = "分页获取用户列表")
    @GetMapping("/page")
    public R<Page<SysUserVo>> page (PageVo pageVo){
        Page<SysUserVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<SysUserVo> pageData = sysUserService.loadPageVo(page);
        return R.success(pageData);
    }
    @Operation(summary = "用户列表")
    @GetMapping("/list")
    public R<List<SysUser>> list (){
        List<SysUser> list = sysUserService.list();
        return R.success(list);
    }
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary="保存用户")
    @PostMapping("/save")
    public  R save(@RequestBody  SysUser sysUser){
        sysUserService.save(sysUser);
        return R.success();
    }
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary="更新用户")
    @PostMapping("/update")
    public  R update(@RequestBody SysUser sysUser){
        sysUserService.updateById(sysUser);
        return R.success();
    }
    @Operation(summary="用户详情")
    @GetMapping("/get/{id}")
    public R<SysUser> detail(@PathVariable String id){
        SysUser sysUser = sysUserService.getById(id);
        return R.success(sysUser);
    }


}
